<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>后台管理模板</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="../../layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="../../css/global.css" media="all">
    <link rel="stylesheet" type="text/css" href="http://www.jq22.com/jquery/font-awesome.4.6.0.css">
    <link rel="stylesheet" href="../../datatables/media/css/jquery.dataTables.css">
    <link rel="stylesheet" href="../../datatables-plugins/integration/bootstrap/3/dataTables.bootstrap.css">
</head>
<body>
    <div class="layui-layout layui-layout-admin" style="border-bottom: solid 5px #1aa094;">
        <!--头部导航-->
        <div class="layui-header header header-demo" ></div>
        <!--左侧导航-->
        <div class="layui-side layui-bg-black" id="admin-side">
        </div>
        <!--右侧具体内容-->
        <div class="layui-body" style="bottom: 0;border-left: solid 2px #1AA094;padding:20px" id="admin-body">
            <div>
                <label class="title">管理员账号管理</label>
                <a href="addmanager.html"><button class="layui-btn layui-btn-small" style="float: right;margin-bottom: 10px"><i class="layui-icon">&#xe608;</i> 添加</button></a>
            </div>
            <hr />
            <div class="select">
                <select name="managerselect" lay-verify="" id="managerSequence">
                    <option value="10">默认</option>
                    <option value="2">只看超级管理员</option>
                    <option value="3">只看普通管理员</option>
                    <option value="4">按创建时间↑</option>
                    <option value="5">按创建时间↓</option>
                </select>
                <div class="layui-btn layui-btn-danger  layui-btn-small" id="managercloseall">批量关闭</div><div class="layui-btn  layui-btn-small " id="managerresetall">重置密码</div>
            </div>
            <div class="layui-form">
                <table class="layui-table" id="managerTable">
                    <thead>
                        <tr>
                            <th><input type="checkbox" name="" id="checkall" onchange="checkAll(this,'#managerTable')"></th>
                            <th style="width: 80px">头像</th>
                            <th>编号</th>
                            <th>用户名</th>
                            <th>类型</th>
                            <th>性别</th>
                            <th>创建时间</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td><input type="checkbox" name="managercheck" id="" ></td>
                            <td><img src="../../images/0.jpg"></td>
                            <td>0412</td>
                            <td>lzy</td>
                            <td>超级管理员</td>
                            <td>女</td>
                            <td>2017-6-30</td>
                            <td>使用中</td>
                            <td><div class="layui-btn layui-btn-danger  layui-btn-small managerclose">冻结账号</div><div class="layui-btn  layui-btn-small reset_psw">重置密码</div><div class="layui-btn layui-btn-warm  layui-btn-small item_edit">编辑</div></td>
                        </tr>
                        <tr>
                            <td><input type="checkbox" name="managercheck" id="" ></td>
                            <td><img src="../../images/xiaosong.jpg"></td>
                            <td>042</td>
                            <td>adj</td>
                            <td>超级管理员</td>
                            <td>男</td>
                            <td>2017-6-30</td>
                            <td>使用中</td>
                            <td><div class="layui-btn layui-btn-danger  layui-btn-small managerclose">冻结账号</div><div class="layui-btn  layui-btn-small reset_psw">重置密码</div><div class="layui-btn layui-btn-warm  layui-btn-small item_edit">编辑</div></td>
                        </tr>
                        <tr>
                            <td><input type="checkbox" name="managercheck" id="" ></td>
                            <td><img src="../../images/1.jpg"></td>
                            <td>0412</td>
                            <td>zal</td>
                            <td>超级管理员</td>
                            <td>女</td>
                            <td>2017-7-1</td>
                            <td>使用中</td>
                            <td><div class="layui-btn layui-btn-danger  layui-btn-small managerclose">冻结账号</div><div class="layui-btn  layui-btn-small reset_psw">重置密码</div><div class="layui-btn layui-btn-warm  layui-btn-small item_edit">编辑</div></td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
        <!--底部导航-->
        <div class="layui-footer footer footer-demo" id="admin-footer"></div>
        <!--JS模块-->
        <script type="text/javascript" src="../../layui/layui.js"></script>
        <script src="../../js/jquery-1.12.3.min.js"></script>
        <script src="../../js/index.js"></script>
        <script type="text/javascript" src="../../js/jquery.cookie.js"></script>
        <!-- DataTables JavaScript -->
        <script src="../../datatables/media/js/jquery.dataTables.min.js"></script>
        <script>
            /*var data = {
                "code": 200,
                "admins": [{
                    "adminId":12345,
                    "adminUrl": "../../images/find_1.png",
                    "adminName": "0412",
                    "adminNickname": "zylin",
                    "adminPower": 1,
                    "adminSex": "女",
                    "adminRegisterTime": "2017-7-7",
                    "adminState": 1
                        // "adminPwd": "SO25KtpgBz",
                }, {
                    "adminId":12345,
                    "adminUrl": "../../images/demo1.png",
                    "adminName": "0412",
                    "adminNickname": "zylin",
                    "adminPower": 1,
                    "adminSex": "女",
                    "adminRegisterTime": "2017-7-7",
                    "adminState": 1
                }, {
                    "adminId":12345,
                    "adminUrl": "../../images/1.jpg",
                    "adminName": "0412",
                    "adminNickname": "zylin",
                    "adminPower": 1,
                    "adminSex": "女",
                    "adminRegisterTime": "2017-7-7",
                    "adminState": 1
                }]
            }
            */
            $(function() {
                $("#manager").addClass('layui-this'); //对应订单管理的背景色
                $('#managerTable').DataTable({
                    responsive: true,
                    ordering: false,
                    searching: true,
                    "bLengthChange": false,
                    "bRetrieve": true,
                    "bFilter": true //过滤功能
                });
                //alert(123)
                managerSelectChange(1)
                adminCookie()
            })



            

            $(document).on('click', 'tr td:not(:first)', function() {
                if ($(this).siblings().first().children().is(':checked') == false) {
                    $(this).siblings().first().children().prop("checked", true);
                } else {
                    $(this).siblings().first().children().prop("checked", false);
                }
            })

            $(document).on('click', '.item_edit', function() {
                var adminId = this.id;
                window.location.href = "editmanager.html?adminId=" + adminId;
            })

            $(document).on('click', '.managerclose', function() {
                k = $(this).parents("tr").index() + 1;
                var adminNickname = $(this).parents("tr").children('td:nth-child(4)').text()
                var adminid = this.id
                layui.use('layer', function() {
                    var layer = layui.layer;
                    layer.confirm('是否要关闭管理员：' + adminNickname + "?", {
                        icon: 2,
                        title: '提示'
                    }, function(index) {
                        $.ajax({
                            type: "GET",
                            url: "/randomdelivery/AdminController/closeAdmin/"+adminid,
                            data: {
                            },
                            async: true,
                            dateType: "json",
                            contentType: "application/json",
                            success: function(data) {
                                if (data.code == 200) {
                                    $("table#managerTable").find("tr:eq(" + k + ")").children('td:nth-child(8)').text("已关闭").css('color', 'red');
                                    $("table#managerTable").find("tr:eq(" + k + ")").children('td:nth-child(9)').children('.managerclose').text("激活账号").attr('class', 'layui-btn layui-btn-normal  layui-btn-small wakeup');
                                    $("input[name='managercheck']:checked").prop('checked', false)
                                    layer.close(index);
                                }
                            },
                            Error: function() {
                                alert("服务器出错");
                            }
                        });
                    });


                });
            })

            $(document).on('click', '.wakeup', function() {
                k = $(this).parents("tr").index() + 1;
                var managerNickname = $(this).parents("tr").children('td:nth-child(4)').text()
                var managerid = this.id
                layui.use('layer', function() {
                    var layer = layui.layer;
                    layer.confirm('是否要激活：' + managerNickname + "?", {
                        icon: 1,
                        title: '提示'
                    }, function(index) {
                        $.ajax({
                            type: "GET",
                            url: "/randomdelivery/AdminController/openAdmin/" + managerid,
                            data: {},
                            async: true,
                            dateType: "json",
                            contentType: "application/json",
                            success: function(data) {
                                if (data.code == 200) {
                                    $("table#managerTable").find("tr:eq(" + k + ")").children('td:nth-child(8)').text("使用中").css('color', 'black');
                                    $("table#managerTable").find("tr:eq(" + k + ")").children('td:nth-child(9)').children('.wakeup').text("冻结账号").attr('class', 'layui-btn layui-btn-danger  layui-btn-small managerclose');
                                    $("input[name='managercheck']:checked").prop('checked', false)
                                    layer.close(index);
                                }
                            },
                            Error: function() {
                                alert("服务器出错");
                            }
                        });
                    });

                });

            })

            $(document).on('click', '.reset_psw', function() {
                var adminNickname = $(this).parents("tr").children("td:nth-child(3)").text();
                var adminId = this.id
                layui.use('layer', function() {
                    var layer = layui.layer;
                    layer.confirm('是否要重置管理员：' + adminNickname + "的密码？", {
                        icon: 3,
                        title: '提示'
                    }, function(index) {
                        //alert(adminId)
                        $.ajax({
                            url: '/randomdelivery/AdminController/resetPassword/'+adminId,
                            type: 'GET',
                            dataType: 'json',
                            contentType: "application/json",
                            success: function(data) {
                                if (data.code == 200) {
                                    layer.msg("重置成功")
                                    $("input[name='managercheck']:checked").prop('checked', false)
                                    layer.close(index);
                                }
                            },
                            error: function() {
                                alert("服务器出错");
                            }
                        });
                    });

                });

            })

            $("#managercloseall").on('click', function() {
                if ($("input[name='managercheck']:checked").length == 0) {
                    layer.msg("请选择管理员")
                } else {
                    layui.use('layer', function() {
                        var layer = layui.layer;
                        layer.confirm('是否要关闭选中管理员？', {
                            icon: 2,
                            title: '提示'
                        }, function(index) {
                            $("input[name='managercheck']:checked").each(function() {
                                var element = this;
                                $("#checkall").attr("checked",false);
                                if (!isNaN(element.id)) {
                                    // m = $(this).parents("tr").index() + 1; // 获取checkbox所在行的顺序
                                    //alert(element.id)
                                    $.ajax({
                                        type: "GET",
                                        url: "/randomdelivery/AdminController/closeAdmin/" + element.id,
                                        data: {},
                                        async: true,
                                        dateType: "json",
                                        contentType: "application/json",
                                        success: function(data) {
                                            if (data.code == 200) {
                                                $(element).parents("tr").children('td:nth-child(8)').text("已禁用").css('color', 'red')
                                                $(element).parents("tr").children('td:nth-child(9)').children('.managerclose').text("激活账号").attr('class', 'layui-btn layui-btn-normal  layui-btn-small wakeup');
                                                $("input[name='managercheck']:checked").prop('checked', false)
                                                layer.close(index);
                                            }
                                        },
                                        Error: function() {
                                            alert("服务器出错");
                                        }
                                    });
                                }
                            })
                        });
                    });
                }
            });

            $("#managerresetall").on('click', function() 
            {
             if ($("input[name='managercheck']:checked").length == 0) 
             {
                 layer.msg("请选择管理员")
             } else 
             {
                 layui.use('layer', function() 
                 {
                     var layer = layui.layer;
                     layer.confirm('是否要重置选中管理员密码？', 
                     {
                         icon: 3,
                         title: '提示'
                     }, function(index) 
                     {
                         $("input[name='managercheck']:checked").each(function() 
                         {
                             $(".checkAll").attr("checked",false);
                             var element = this;
                             alert(element.id)
                             $.ajax({
                                 type: "GET",
                                 url: "/randomdelivery/AdminController/resetPassword/" + element.id,
                                 data: {},
                                 async: true,
                                 dateType: "json",
                                 contentType: "application/json",
                                 success: function(data) 
                                 {
                                     if (data.code == 200) 
                                     {
                                         layer.msg("重置成功")
                                         $("input[name='managercheck']:checked").prop('checked', false)
                                         layer.close(index);
                                     }
                                 },
                                 Error: function() 
                                 {
                                     alert("服务器出错");
                                 }
                             });
                         })
                     });
                 });
             }
         });


            $("#managerSequence").bind("change", function() {
                managerSelectChange($(this).val())
               // alert(213)
           })

            function managerSelectChange(v) {
             $.ajax({
                 type: "get",
                 url: "/randomdelivery/AdminController/admins/pageCount/1/pageSize/10",
                 data: {
                     "sequence": v,
                 },
                 async: true,
                 dateType: "json",
                 contentType: "application/json",
                 success: function(data) {
                    managerdata(data);

                },
                Error: function() {
                 alert("服务器出错");
             }
         })
         }


         Date.prototype.Format = function (fmt) 
                { //author: meizz 
                    var o = {
                    "M+": this.getMonth() + 1, //月份 
                    "d+": this.getDate(), //日 
                    "h+": this.getHours(), //小时 
                    "m+": this.getMinutes(), //分 
                    "s+": this.getSeconds(), //秒 
                    "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
                    "S": this.getMilliseconds() //毫秒 
                };
                if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
                for (var k in o)
                    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                return fmt;
            }


            function managerdata(data) {
                $('#managerTable').dataTable().fnClearTable(); //清除表格内
                $.each(data.admins, function(index, item) {
                    var power;
                    var adminState;
                    var adminRegisterTime = new Date(item.adminCreatetime).Format("yyyy-MM-dd hh:mm:ss");
                    k = index + 1;
                    if (item.adminPower == 1) {
                        power = "超级管理员"
                    } else {
                        power = "普通管理员"
                    }
                    if (item.adminState == 0) {
                        adminState = "已禁用"
                    } else {
                        adminState = "使用中"
                    }
                    $('#managerTable').dataTable().fnAddData([
                        '<input type="checkbox" name="managercheck" id="' + item.adminId + '" >',
                        '<img src=' + item.adminUrl + '>',
                        item.adminName,
                        item.adminNickname,
                        power,
                        item.adminSex,
                        adminRegisterTime,
                        adminState,
                        '<div class="layui-btn layui-btn-danger  layui-btn-small managerclose" id="' + item.adminId + '">冻结账号</div><div class="layui-btn  layui-btn-small reset_psw" id="' + item.adminId + '">重置密码</div><div class="layui-btn layui-btn-warm  layui-btn-small item_edit" id="' + item.adminId + '">编辑</div>'
                        ]);
                    if (item.adminState == 0) {
                        $("table#managerTable").find("tr:eq(" + k + ")").children('td:nth-child(9)').children('.managerclose').text("激活账号").attr('class', 'layui-btn layui-btn-normal  layui-btn-small wakeup').attr("id",item.adminId);
                    }
                })
            }



        </script>

    </div>
</body>
</html>

